package com.gxc.string;

/**
 * @author 宫新程
 * @since 2019/2/20 11:16
 */
public class To16 {

  public static void main(String[] args) {
    String result = getEslIdByEslCodeOrId("729834081382901287");
    System.out.println(result);
  }

  /**
   * 公用方法2: 根据价签id或者编码获取价签id.
   *
   * @param eslCodeOrId 价签id或者编码
   * @return 价签id
   */
  private static String getEslIdByEslCodeOrId(String eslCodeOrId) {
    // 判断是id 还是编码, 如果不包含"-"表示为价签id
    if (!eslCodeOrId.contains("-")) {
      // 18位数字的条码，把后十位转成16进制的
      String substring = eslCodeOrId.substring(8);
      Long num = Long.parseLong(substring);
      String str = Long.toHexString(num).toUpperCase();
      char[] array = str.toCharArray();
      StringBuilder sb = new StringBuilder();
      for (int i = 1; i <= array.length; i++) {
        sb.append(array[i - 1]);
        if (i % 2 == 0 && i != array.length) {
          sb.append("-");
        }
      }
      return sb.toString();
    }
    return eslCodeOrId;
  }
}
